﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.PO;

namespace Com.CMS263.DAO
{
    public class StudentStudyDAO : BaseService
    {
        public StudentStudyDAO() { }
        public int GetMaxId()
        {
            string hql = "from StudentStudy order by Id desc";
            try
            {
                StudentStudy prod = FindList<StudentStudy>(hql, null, 0, 1).First();
                return prod.Id + 1;
            }
            catch (Exception)
            {

                return 1;
            }
        }
        public IList<StudentStudy> GetListByPager(int page, int pagesize, int userid)
        {
            string hql = " from StudentStudy where 1=1";
            if (userid > 0)
            {
                hql += " and StudentInfoId=" + userid;
            }
            hql += " order by  AddDate Desc,Id Desc";
            try
            {
                if (page == -1)
                {
                    return FindList<StudentStudy>(hql, null, null, null);
                }
                else
                {
                    return FindList<StudentStudy>(hql, null, (page - 1) * pagesize, pagesize);
                }
            }
            catch (Exception)
            {

                return null;
            }

        }
        public StudentStudy GetStudy(DateTime time, int userid)
        {
            string hql = " from StudentStudy where 1=1";
            hql += " and CONVERT(varchar(12) , AddDate, 102 )='" + time.ToString("yyyy.MM.dd") + "'";
            if (userid > 0)
            {
                hql += " and StudentInfoId=" + userid;
            }
            hql += " order by  AddDate Desc,Id Desc";
            try
            {

                return FindList<StudentStudy>(hql, null, 0, 1)[0];

            }
            catch (Exception)
            {

                return new StudentStudy();
            }

        }

        public int GetListByPager(int userid)
        {
            string hql = "select count(*) from StudentStudy where 1=1";
            if (userid > 0)
            {
                hql += " and StudentInfoId=" + userid;
            }
            try
            {
                return Convert.ToInt32(GetValue(hql));
            }
            catch (Exception)
            {
                return 0;
            }

        }
    }
}
